package org.jhub1.system.xmpp.commands;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.jhub1.system.xmpp.config.ConfigAdapter;
import org.jhub1.system.xmpp.gateway.packet.SubItemable;
import org.jhub1.system.xmpp.log.LogAdapter;
import org.jhub1.system.xmpp.proxy.HTTPGatewayRequest;
import org.jhub1.system.xmpp.proxy.ProcessResponse;

public class CommandExecutor {

	private static HTTPGatewayRequest hgr;
	private static LogAdapter log = LogAdapter.getLog(CommandExecutor.class);
	private static final Map<String, String> commands;
	private String source;
	// http://192.168.1.72:8180/dbusMethod/getNewUSBHubs
	/*
	 * static { Map<String, String> aMap = new HashMap<String, String>();
	 * aMap.put("_HUBSLIST_", "/earthquakes/feed/geojsonp/significant/week");
	 * aMap.put("_ALL_", "/earthquakes/feed/geojsonp/significant/week");
	 * aMap.put("_SET_", "/earthquakes/feed/geojsonp/significant/week");
	 * aMap.put("_GETHUB_", "/earthquakes/feed/geojsonp/significant/week");
	 * commands = Collections.unmodifiableMap(aMap); }
	 */
	static {
		Map<String, String> aMap = new HashMap<String, String>();
		aMap.put("_HUBSLIST_", "/dbusMethod/getNewUSBHubs");
		aMap.put("_ALL_", "/dbusMethod/getAllDevices");
		aMap.put("_SET_", "/earthquakes/feed/geojsonp/significant/week");
		aMap.put("_GETHUB_", "/earthquakes/feed/geojsonp/significant/week");
		commands = Collections.unmodifiableMap(aMap);
	}

	public CommandExecutor(ConfigAdapter config, String source) {
//		hgr = new HTTPGatewayRequest(config.getGatewayHost(),
	//			config.getGatewayPort());
		this.source = source;
	}

	public ProcessResponse executeComTask(String command,
			Iterator<SubItemable> items) {
		ProcessResponse pr = null;
		if (command.equals("_HUBSLIST_")) {
			log.debug("[" + source + "] Requested command: _HUBSLIST_");
			pr = hgr.getResult("GET", commands.get("_HUBSLIST_"), null);
		} else if (command.equals("_ALL_")) {
			log.debug("[" + source + "] Requested command: _ALL_");
			pr = hgr.getResult("GET", commands.get("_ALL_"), null);
		} else if (command.equals("_SET_")) {
			log.debug("[" + source + "] Requested command: _SET_");
			pr = hgr.getResult("GET", commands.get("_SET_"), items);
		} else if (command.startsWith("_GETHUB_")) {
			log.debug("[" + source + "] Requested command: " + command);
			String[] parts = command.split("\\:");
			if (parts.length >= 2) {
				if (parts[1].length() > 0) {
					String hubid = parts[1];
					pr = hgr.getResult("GET", commands.get("_GETHUB_") + "/"
							+ hubid, null);
				}
			} else {
				pr = new ProcessResponse();
				pr.setError("MISSING_HUBID");
			}
		} else {
			pr = new ProcessResponse();
			pr.setError("COMMAND_NOT_RECOGNIZED");
		}
		return pr;
	}

	public ProcessResponse executeDirTask(String requestType, String uri,
			Iterator<SubItemable> items) {
		ProcessResponse pr = null;
		if (requestType.equalsIgnoreCase("GET")) {
			log.debug("[" + source + "] Requested GET for: " + uri);
			pr = hgr.getResult("GET", uri, items);
		} else if (requestType.equalsIgnoreCase("POST")) {
			log.debug("[" + source + "] Requested POST for: " + uri);
			pr = hgr.getResult("POST", uri, items);
		} else {
			pr = new ProcessResponse();
			pr.setError("REQUEST_TYPE_NOT_RECOGNIZED");
		}
		return pr;
	}

}
